#!/bin/sh
# Written at 2015.04.15 by Kevin 
# Included command : start, stop, restart, status

this_dir=`pwd`
dirname $0|grep "^/" >/dev/null
if [ $? -eq 0 ];then
    this_dir=`dirname $0`
else
    dirname $0|grep "^\." >/dev/null
    retval=$?
    if [ $retval -eq 0 ];then
        this_dir=`dirname $0|sed "s#^.#$this_dir#"`
    else
        this_dir=`dirname $0|sed "s#^#$this_dir/#"`
    fi
fi

# echo $this_dir

service_config_file=${this_dir}/services.json
php_ini_file=${this_dir}/php.ini
php_exe=`which php`
pidfile=${this_dir}/logs/mypid

stop_timeout=30

start() {
    # find ${this_dir} -name '.svn' | xargs rm -rf
    # export CONFIGD_LOG_FILE=/usr/local/whistle/configd/logs/configd.log
    # export LD_LIBRARY_PATH=/usr/local/whistle/configd/libs:$LD_LIBRARY_PATH
    #[ -x $exec ] || exit 5
    # [ ! -d ${runpath} ] && echo "Create Run Dir: ${runpath}"; (mkdir -p ${runpath}; chown -R ${user}:${user} ${runpath} || exit 6)
    #export USE_ZEND_ALLOC=0
    echo -n $"Starting soa server: "
    #valgrind --trace-children=yes --track-fds=yes --show-below-main=yes --show-reachable=yes --read-var-info=yes --leak-check=full --track-origins=yes --log-file=/root/logs/valgrind.%p.log ${php_exe} -c ${php_ini_file} -f ${this_dir}/app/wroot/SoaServer.php
    ${php_exe} -c ${php_ini_file} -f ${this_dir}/app/core/SoaServer.php
    sleep 1
    processexist=0
    if [ -e $pidfile ]; then
        pidfileexist=1
        processpid=`cat $pidfile`
        processcount=`ps -p ${processpid} | wc -l`
        if [ ${processcount} -lt 2 ]; then
            processexist=0
        else
            processexist=1
        fi
    fi
    if [ $processexist -eq 1 ]; then
        echo "successfully"
    else
        echo "failed"
    fi
}

stop() {
    echo "unregister service node ..."
	stools -c ${php_ini_file} -a delete -d
	sleep 3
	echo "unregister service node done!"
    echo -n $"Stopping soa server: "
    processpid=0
    processexist=0
    if [ -e $pidfile ]; then
        pidfileexist=1
        processpid=`cat $pidfile`
        processcount=`ps -p ${processpid} | wc -l`
        if [ ${processcount} -lt 2 ]; then
            processexist=0
        else
            processexist=1
        fi
    fi
    if [ $processexist -eq 1 ]; then
        kill -TERM ${processpid}
        sleep 1
    fi
    processexist=0
    if [ -e $pidfile ]; then
        pidfileexist=1
        processpid=`cat $pidfile`
        processcount=`ps -p ${processpid} | wc -l`
        if [ ${processcount} -lt 2 ]; then
            processexist=0
        else
            processexist=1
        fi
    fi
    if [ $processexist -eq 1 ]; then
        echo "failed"
    else
        echo "successfully"
    fi
}

restart() {
    stop
    start
}

status() {
    echo -n $"Soa server: "
    processpid=0
    processexist=0
    if [ -e $pidfile ]; then
        pidfileexist=1
        processpid=`cat $pidfile`
        processcount=`ps -p ${processpid} | wc -l`
        if [ ${processcount} -lt 2 ]; then
            processexist=0
        else
            processexist=1
        fi
    fi
    if [ $processexist -eq 1 ]; then
		echo "running"
    else
        echo "down"
    fi
    #cat $pidfile | xargs pstree -a -p
}

prepare() {
    find ${this_dir} -name '.svn' | xargs rm -rf
}

clear(){
	rm -rf ${this_dir}/logs/*
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  status)
    status
    ;;
  prepare)
    prepare
    ;;
  clear)
    clear
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status|prepare|clear}"
    exit 1
esac

exit $?
